cmake_minimum_required(VERSION 3.5)
add_definitions(-w)
set(CMAKE_CUDA_ARCHITECTURES 60 61 62 70 72 75 86)
set(CMAKE_CUDA_COMPILER /usr/local/cuda/bin/nvcc)
project(camera_d455)
enable_language(CXX CUDA)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -O3 -g")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_BUILD_TYPE Release)
option(CUDA_USE_STATIC_CUDA_RUNTIME OFF)

SET(CMAKE_BUILD_TYPE "Debug")
SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")

LIST(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

# find dependencies
find_package(ament_cmake REQUIRED) # ROS2中基于CMake的软件包的构建系统
find_package(rosidl_default_generators REQUIRED) # 定义默认ROS接口生成器的配置包
find_package(std_msgs REQUIRED)
find_package(sensor_msgs REQUIRED)    
find_package(rclcpp REQUIRED) # rclcpp提供了与ROS2交互的标准 C++ API      
find_package(base_interface REQUIRED)              

# ament_export_dependencies(rosidl_default_runtime)

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  ament_lint_auto_find_test_dependencies()
endif()

ament_package()

set(Eigen3_INCLUDE_DIRS /usr/include/eigen3)
find_Package(Eigen3 REQUIRED)
find_Package(Ceres REQUIRED)
find_Package(CSparse REQUIRED)


# CUDA
set(CUDA_DIR /usr/local/cuda)
find_package(CUDA REQUIRED)

# OpenCV
set(OpenCV_DIR /usr/local/share/opencv4)
find_package(OpenCV REQUIRED)

# PCL
set(PCL_DIR /usr/local/share/pcl-1.13)
find_package(PCL REQUIRED)

# realsense
set(realsense2_DIR /usr/lib/x86_64-linux-gnu/cmake/realsense2)
find_package(realsense2 REQUIRED)

# gflags
find_package(gflags REQUIRED)
 
# tensorrt
set(TensorRT_INCLUDE_DIRS /home/action/TensorRT-8.5.1.7.Linux.x86_64-gnu.cuda-11.8.cudnn8.6/TensorRT-8.5.1.7/include)
set(TensorRT_LIBRARIES /home/action/TensorRT-8.5.1.7.Linux.x86_64-gnu.cuda-11.8.cudnn8.6/TensorRT-8.5.1.7/targets/x86_64-linux-gnu/lib)

message("Eigen3 library status:")
message("    config: ${realsense2_DIR}")
message("    version: ${realsense2_VERSION}")
message("    libraries: ${realsense2_LIBRARY_DIRS}")
message("    include path: ${realsense2_INCLUDE_DIRS}")

list(APPEND INCLUDE_DIRS
${CUDA_INCLUDE_DIRS}
${realsense2_INCLUDE_DIR} 
${OpenCV_INCLUDE_DIRS}
${PCL_INCLUDE_DIRS}
${TensorRT_INCLUDE_DIRS}
        ./include
        )

list(APPEND ALL_LIBS
${CUDA_LIBRARIES}
${OpenCV_LIBS}
${PCL_LIBRARIES}
${realsense2_LIBRARY}
${TensorRT_LIBRARIES}
        )

include_directories(${INCLUDE_DIRS} ${CMAKE_SOURCE_DIR}/include ${EIGEN3_INCLUDE_DIR} ${CSPARSE_INCLUDE_DIR})
include_directories(/opt/ros/humble/include/)
include_directories(/opt/ros/humble/include/message_filters)
include_directories(/opt/ros/humble/include/pcl_msgs)

file(GLOB SOURCE_LIST "${CMAKE_SOURCE_DIR}/src/*.cpp")
add_executable(camera_d455 ${SOURCE_LIST})
target_link_libraries(camera_d455 nvinfer nvinfer_plugin cudart k4a k4arecord gflags ${ALL_LIBS} ${CERES_LIBRARIES})
# target_include_directories(camera_d455 PUBLIC /opt/ros/humble/include/message_filters/message_filters)

ament_target_dependencies(camera_d455 
rclcpp
sensor_msgs
base_interface
std_msgs)

install(TARGETS
camera_d455
DESTINATION lib/${PROJECT_NAME})
